/*
 * PictureScanFragment.java
 * business
 *
 * Created by ChenTao on 2017/5/16.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.picturescan;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.base.BaseFragment;
import com.yidiandao.business.constant.Intents;
import com.yidiandao.business.manager.ImageManager;
import com.yidiandao.business.ui.adapter.PictureScanAdapter;
import com.yidiandao.business.ui.widget.view.ViewPagerFixed;
import com.yidiandao.business.utils.UIUtils;

/** 用于图片浏览 */
public class PictureScanFragment extends BaseFragment implements PictureScanContract.View {

    private ViewPagerFixed mViewPager;

    private Toolbar mToolbar;

    private TextView mTvDelete;

    private int currentIndex;

    private PictureScanAdapter mAdapter;

    private TextView mTvSize;
    private int total;

    public static PictureScanFragment getInstance(Bundle extras) {
        PictureScanFragment fragment = new PictureScanFragment();
        fragment.setArguments(extras);
        return fragment;
    }

    @Override
    public void setPresenter(PictureScanContract.Presenter presenter) {
    }

    @Override
    public String getPagerTag() {
        return PictureScanFragment.class.getSimpleName();
    }

    @Override
    public void showToast(String text) {
    }

    @Override
    protected void initViews() {

        mTvSize = (TextView) mRootView.findViewById(R.id.tv_size);

        mToolbar = (Toolbar) mRootView.findViewById(R.id.toolbar);
        mTvDelete = (TextView) mRootView.findViewById(R.id.tv_delete);

        mViewPager = (ViewPagerFixed) mRootView.findViewById(R.id.view_pager);
        mAdapter = new PictureScanAdapter(mActivity);
        mViewPager.setAdapter(mAdapter);

        initData();
        initEvent();
    }

    private void initData() {
        //是否显示删除按钮
        boolean isShowDeleteBtn = getArguments().getBoolean(Intents.SHOW_DELETE_BTN);
        mTvDelete.setVisibility(isShowDeleteBtn ? View.VISIBLE : View.GONE);

        //选中第几页
        int index = getArguments().getInt(Intents.INDEX);
        currentIndex = index;
        mViewPager.setCurrentItem(index);

        //设置当前页  总页数
        total = ImageManager.getInstance().getFinalImageList().size();
        mTvSize.setText(UIUtils.getString(R.string.ask_to_buy_size, currentIndex + 1, total));
    }

    private void initEvent() {
        mToolbar.setNavigationOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mActivity.finish();
                    }
                });
        mViewPager.addOnPageChangeListener(
                new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageScrolled(
                            int position, float positionOffset, int positionOffsetPixels) {
                    }

                    @Override
                    public void onPageSelected(int position) {
                        currentIndex = position;
                        mTvSize.setText(
                                UIUtils.getString(
                                        R.string.ask_to_buy_size, currentIndex + 1, total));
                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {
                    }
                });
        mTvDelete.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AlertDialog.Builder dialog = new AlertDialog.Builder(mActivity);
                        dialog.setTitle(UIUtils.getString(R.string.dialog_tip5));
                        dialog.setMessage(UIUtils.getString(R.string.dialog_tip6));
                        dialog.setPositiveButton(
                                UIUtils.getString(R.string.dialog_sure),
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {

                                        ImageManager.getInstance()
                                                .getFinalImageList()
                                                .remove(currentIndex);
                                        ImageManager.getInstance()
                                                .getCurrentImageList()
                                                .remove(currentIndex);
                                        try {
                                            ImageManager.getInstance().getImageId().remove(currentIndex);
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }

                                        if (mAdapter.getCount() == 0) {
                                            mActivity.finish();
                                            return;
                                        }

                                        mAdapter.notifyDataSetChanged();
                                        //重新设置图片的位置和总数
                                        total =
                                                ImageManager.getInstance()
                                                        .getFinalImageList()
                                                        .size();
                                        mTvSize.setText(
                                                UIUtils.getString(
                                                        R.string.ask_to_buy_size,
                                                        currentIndex + 1,
                                                        total));
                                    }
                                })
                                .setNegativeButton(UIUtils.getString(R.string.dialog_cancel), null)
                                .show();
                    }
                });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_picture_scan;
    }
}
